package servlet;

import entity.Product;
import service.ProductService;
import service.impl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(urlPatterns = "/ProductUpdateServlet")
public class ProductUpdateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        int id = Integer.parseInt(request.getParameter("id"));
        String product_name = request.getParameter("product_name");
        Double product_price = Double.valueOf(request.getParameter("product_price"));
        String product_desc = request.getParameter("product_desc");
        int flag = Integer.parseInt(request.getParameter("flag"));


        Product product = new Product();
        product.setId(id);
        product.setProduct_desc(product_desc);
        product.setProduct_price(product_price);
        product.setFlag(flag);
        product.setProduct_name(product_name);

        ProductService productService = new ProductServiceImpl();
        int i = productService.update(product);
        if (i>0){
            request.getRequestDispatcher("ProductSelectServlet").forward(request,response);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

